<template>
  <div style="height: 6vh;background-color:#fff; padding: 10px 20px;">
    <span style="line-height: 60px;font-size: 20px;">用户管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px;"
               @click="addUser">新建用户
    </el-button>
    <!--   :visible.sync="dialogVisible"
          :before-close="handleClose(done)"-->
    <el-dialog
        :title="dialogTitle"
        style="width: 1000px;padding:40px;"
        v-model="dialogVisible"
        :before-close="handleClose"
    >
      <el-form label-width="80px" label-position="top">

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="用户名">
              <el-input v-model="saveUserForm.username" placeholder="请输入用户名" :disabled="saveUserForm.id!=null"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号">
              <el-input v-model="saveUserForm.phone" placeholder="请输入手机号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input v-model="saveUserForm.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄">
              <el-input v-model="saveUserForm.age" placeholder="请输入年龄"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="职级">
              <el-select v-model="saveUserForm.level" placeholder="请选择" @change="loadLeader">
                <el-option
                    v-for="item in optionsList"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="直属领导">
              <el-select v-model="saveUserForm.parentId" placeholder="请选择">
                <el-option v-for="item in leaderOptions" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="性别">
              <el-radio-group v-model="saveUserForm.gender">
                <el-radio border label="男" value="1" style="margin:0;"></el-radio>
                <el-radio border label="女" value="0"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="用户状态">
              <el-radio-group v-model="saveUserForm.status">
                <el-radio border label="启用" value="1" style="margin:0;"></el-radio>
                <el-radio border label="禁用" value="0"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>


        </el-row>

      </el-form>
      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="saveUser();">保存</el-button>
      </template>


      <!--    <span slot="footer" class="dialog-footer">-->
      <!--      <el-button @click="dialogVisible = false">取 消</el-button>-->
      <!--      <el-button type="primary" @click="saveUser">确 定</el-button>-->
      <!--    </span>-->
    </el-dialog>
<!--用户搜索卡片-->
    <el-card style="margin: 20px; height: 70px">
      <el-form label-width="80px" inline="true">
        <el-form-item label="用户名" style="width: 270px;">
          <el-input v-model="searchUserForm.username" placeholder="请输入用户名" @keydown.enter="loadUser"></el-input>
        </el-form-item>
        <el-form-item label="状态" style="width: 270px;">
          <el-select v-model="searchUserForm.status" placeholder="请选择" @change="loadUser">
            <el-option
                label="启用"
                value="1">
            </el-option>
            <el-option
                label="禁用"
                value="0">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="primary" @click="loadUser" >查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

<!--    用户table卡片-->
    <el-card style="margin: 20px;">
      <el-table :data="userArr"
          style="width: 100%">
        <el-table-column header-align="center"  width="80" align="center" type="index" label="编号">
        </el-table-column>

        <el-table-column header-align="center"   align="center" prop="username" label="用户名">

        </el-table-column>

        <el-table-column header-align="center"  align="center" prop="phone" label="手机号码">

        </el-table-column>

        <el-table-column header-align="center"  align="center" prop="createTime" label="创建时间">

        </el-table-column>

        <el-table-column header-align="center" align="center" prop="status" label="状态">
          <template #default="scope">
            <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" @change="updateStatus(scope.row)" ></el-switch>
          </template>
        </el-table-column>

        <el-table-column header-align="center" align="center"  label="操作">
          <template #default="scope">
            <el-button type="primary" link :disabled="scope.row.level==40" @click="editUser(scope.row)">编辑</el-button>
            <el-button type="danger" link :disabled="scope.row.level==40" @click="deleteUser(scope.row)">删除</el-button>
            <el-button type="primary" link :disabled="scope.row.level==40" @click="resetPassword(scope.row)">密码重置</el-button>
          </template>
        </el-table-column>


      </el-table>
    </el-card>

  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

const dialogVisible = ref(false);
const optionsList = ref([
  {
    value: '10',
    label: '员工'
  },
  {
    value: '20',
    label: '经理'
  },
  {
    value: '30',
    label: '总监'
  },
  {
    value: '40',
    label: '总裁'
  }
]);


const searchUserForm = ref({username: '', status: ''});
const userArr = ref([]);
//加载user表
const loadUser = () => {
  console.log('loadUser启用了');
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASE_URL + '/v1/user/select?'+data).then(response => {
    if(response.data.code==2000){
      ElMessage.success(response.data.msg);
      // console.log(response.data.data);
      userArr.value = response.data.data;
      // console.log(userArr.value);
    }else {
      ElMessage.error(response.data.msg);
    }
  });
};
onMounted(()=>{
  loadUser();
})
const resetSearch = () => {
  searchUserForm.value = {username: '', status: ''};
  loadUser();
};

const saveUserForm = ref(
    {
      id: '',
      username: '',
      password: '',
      phone: '',
      email: '',
      age: '',
      gender: '',
      level: '',
      parentId: '',
      status: ''
    }
);
const saveUser = () => {
  console.log(saveUserForm.value);
  //用qs插件将json对象转成查询字符串了 那么后端就不用再做json转字符串了 不需要@RequestBody了
  // let data = qs.stringify(saveUserForm.value);
  // console.log(data)
  axios.post(BASE_URL + '/v1/user/save', saveUserForm.value).then(response => {
    if(response.data.code==2000){
      ElMessage.success('保存成功');
    }else {
      ElMessage.error(response.data.msg);
    }
  });
  loadUser();
  saveUserForm.value = {};
//
//   axios.post(BASE_URL + '/v1/user/save', saveUserForm.value).then(response => {
//     if(response.data.code==2000){
//       ElMessage.success('保存成功');
//     }else {
//       ElMessage.error(response.data.msg);
//     }
// });
};

//定义处理关闭弹窗的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭本弹窗吗?')){
    saveUserForm.value = {};
    dialogVisible.value = false;
  }
}

const leaderOptions = ref([]);
//定义领导数组的数据
const loadLeader = ()=>{
  saveUserForm.value.parentId = '';
  let level= parseInt(saveUserForm.value.level)+10;
  axios.get(BASE_URL+'/v1/user/select?level='+level)
      .then((response)=>{
        if(response.data.code == 2000){
          leaderOptions.value = response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

const dialogTitle = ref('新建用户');
//新建用户按钮显示的方法
const addUser = ()=>{
  dialogTitle.value = '新建用户';
  dialogVisible.value = true;
}
const editUser = (row)=>{
  dialogTitle.value = '编辑用户';
  dialogVisible.value = true;
  saveUserForm.value = row;
  let level= parseInt(saveUserForm.value.level)+10;
  axios.get(BASE_URL+'/v1/user/select?level='+level)
      .then((response)=>{
        if(response.data.code == 2000){
          leaderOptions.value = response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

//重置密码
const resetPassword = (row)=>{
  axios.post(BASE_URL+'/v1/user/resetPassword/'+row.id)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success(response.data.msg);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

//更新状态
const updateStatus = (row)=>{
  axios.post(BASE_URL+'/v1/user/updateStatus/'+row.id+'/'+row.status)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success(response.data.msg);
          loadUser();
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

//删除用户信息
const deleteUser = (row)=>{
  if(confirm('您确认要删除'+row.username+'用户吗?')){
    axios.post(BASE_URL+'/v1/user/delete/'+row.id)
        .then((response)=>{
          if(response.data.code == 2000){
            ElMessage.success(response.data.msg);
            loadUser();
          }else{
            ElMessage.error(response.data.msg);
          }
        })
  }
}

</script>

<style scoped>

</style>